<?php

/**
 * smart_report actions.
 *
 * @package    sf_sandbox
 * @subpackage smart_report
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class smart_reportActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->form = new SmartReportForm();

        $export_to_word = $request->hasParameter('export');

        if ($request->isMethod('POST') || $export_to_word) {

            if ($export_to_word) {
                $values = unserialize($this->getUser()->getAttribute('smart_report_values'));
            } else {
                $values = $request->getPostParameters();
                $this->getUser()->setAttribute('smart_report_values', serialize($values));
            }

            if (isset($values['draw_chart']))
                $this->forward('smart_report', 'chart');

            $kind = $values['output'];
            $class = $kind . 'FormFilter';
            unset($values['output']);
            $this->filter = new $class();
            $q = $this->filter->doBuildQueryForReport($values);

            if ($q instanceOf PDOStatement)
                $this->results = $q->fetchAll(Doctrine_Core::FETCH_ASSOC);
            else
                $this->results = $q->fetchArray();

            if ($kind == 'Trainer') {
                foreach ($this->results as $k => $one) {
                    $this->results[$k]['absence_more_than_20'] = TrainerTable::getInstance()->countAbsentTeachersForProgram($this->results[$k]['prog_id']);
                    unset($this->results[$k]['Programs']);
                }
            }
            if ($kind == 'Program') {
                foreach ($this->results as $k => $one) {
                    $obj = LocationsInSectorTable::getInstance()->find($this->results[$k]['location_of_holding']);
                    $this->results[$k]['location_of_holding'] = is_object($obj) ? $obj->getExtendedString() : '';
                }
            }
            if ($kind == 'Teacher') {
                foreach ($this->results as $k => $one) {
                    $this->results[$k]['absence_percentage'] = TeacherTable::getInstance()->getAbsenceCountPercentageById($this->results[$k]['intg_identity']);
                }
            }

            $this->setTemplate("results");
        }

        $this->show_for_array = array(
            'LocationsInSector' => 'name'
            , 'CodeTarget' => 'title'
            , 'CodeSector' => 'name'
            , 'LinkTeacherProgram' => '[COUNT]'
            , 'Teachers' => 'f_name' // array('f_name','m_name') ...
            , 'School' => 'name'
        );

        $this->labels = array(
            'program_name' => 'Program name'
            , 'is_mandatory' => 'Mandatory'
            , 'start_date' => 'Start date'
            , 'end_date' => 'End date'
            , 'CodeTarget' => 'Code target list'
            , 'CodeSector' => 'Code sector list'
            , 'LocationsInSector' => 'Location of holding'
            , 'LinkTeacherProgram' => 'Assinger number' // assignee
            , 'teacher_name' => 'Teacher'
            , 'School' => 'school name'
            , 'trainer_name' => 'Trainer Name'
            , 'assignee_count' => 'Assignee Count'
            , 'absence_more_than_20' => 'absence_more_than_20'
            , 'location' => 'program location'
            , 'pname' => 'program name'
            , 'school_name' => 'school name'
            , 'teacher_count' => 'teacher_count'
            , 'precentage' => 'trainee percentage'
            , 'intg_identity' => 'teacher identity'
            , 'name' => 'Name'
            , 'location_of_holding' => 'Location of holding'
            , 'duration_hours' => 'Duration hours'
            , 'count_of_assigned_programs' => 'Count of assigned programs'
            , 'absence_percentage' => 'Absence Percentage'
            , 'sector_name' => 'Sector'
        );

        $this->export_to_word = $export_to_word;
        if ($export_to_word) {
            $time = time();
            $rnd = substr($time, strlen($time) - 4, strlen($time));
            header('Content-type: application/doc');
            header('Content-Disposition: attachment; filename="' . strtolower($kind) . '_report_' . $rnd . '.doc"');
            header('Content-Description: File Transfer');
        }
    }

    public function executeChart(sfWebRequest $request) {
        $values = unserialize($this->getUser()->getAttribute('smart_report_values'));
        $this->data = SchoolTable::getInstance()->getChartData($values);
        $this->setLayout('chart');
    }

}
